﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.Components;
using Aliyeye.Car.Net.UI.PopupDialog;
using Aliyeye.Car.Net.UI.Common;

namespace Aliyeye.Car.Net.UI.SystemModel
{
    public partial class Remind : Aliyeye.Car.Net.UI.BaseForm
    {
        RemindSvc.SoapHeaderClass sopaHeader = new RemindSvc.SoapHeaderClass();
        private String strID = "";

        public Remind()
        {
            InitializeComponent();
        }

        private void Role_Load(object sender, EventArgs e)
        {
            //BLL.Accounts_Roles bll = new BLL.Accounts_Roles();
            //dataGridView1.DataSource = bll.GetList().Tables[0];

            this.pager1.PageCurrent = 1;
            this.pager1.Bind();

        }

        private int dgvBind()
        {
            string _strSql = "";
            string strWhere = "";
            PageData pageData = new PageData(this, pager1, overlay);
            pageData.TableName = "Remind";
            pageData.PrimaryKey = "Id";
            pageData.OrderStr = "Id desc";
            pageData.PageIndex = this.pager1.PageCurrent;
            pageData.PageSize = this.pager1.PageSize;
            pageData.QueryCondition = _strSql + strWhere.ToString();
            pageData.QueryFieldName = "*";

            DataSet ds = pageData.QueryDataTable();
            dgvInfo.AutoGenerateColumns = false;
            //if (ds != null)
            //{
            //    this.pager1.bindingSource.DataSource = ds.Tables[0];
            //    this.pager1.bindingNavigator.BindingSource = pager1.bindingSource;
            //}
            dgvInfo.DataSource = this.pager1.bindingSource;
            return pageData.TotalCount;
        }

        private int pager1_EventPaging(EventPagingArg e)
        {
            return dgvBind();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (IsANonHeaderButtonCell(e))
            {
                ButtonClicked(e);
            }
        }

        private void ButtonClicked(DataGridViewCellEventArgs e)
        {
            String strButtonContent = dgvInfo.Columns[e.ColumnIndex].Name;
            String id = dgvInfo.Rows[e.RowIndex].Cells["编号"].Value.ToString();
            String timeValue = dgvInfo.Rows[e.RowIndex].Cells["HaveTime"].Value.ToString();
            string name = dgvInfo.Rows[e.RowIndex].Cells["提醒项目"].Value.ToString();
            if (strButtonContent.Trim().Equals("修改"))
            {
                strID = id;
                lblName.Text = name;
                txtHaveTime.Text = timeValue;
                panelUpdate.Visible = true;
            }
        }

        private bool IsANonHeaderButtonCell(DataGridViewCellEventArgs cellEvent)
        {
            if (dgvInfo.Columns[cellEvent.ColumnIndex] is
                 DataGridViewButtonColumn &&
                 cellEvent.RowIndex != -1)
            { return true; }
            else { return (false); }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            panelUpdate.Visible = false;
        }

        //修改
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtHaveTime.Text))
            {
                DialogWarning dia = new DialogWarning("提醒单位不能为空！");
                dia.ShowDialog();
                return;
            }

            if (strID != "")
            {
                overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
                RemindSvc.Remind model = new RemindSvc.Remind();
                model.Id = Convert.ToInt32(strID);
                model.Name = lblName.Text;
                model.HaveTime = Convert.ToInt32(txtHaveTime.Text);

                ServiceManipulator.SafeAsyncExecution<RemindSvc.RemindServiceSoapClient>(
                    proxy => proxy.Update(sopaHeader, model),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            PageCommon.Notify("修改提醒成功！");
                            overlay.FadeOut();Cursor.Current = Cursors.Default;
                            strID = "";
                            lblName.Text = "";
                            txtHaveTime.Text = "";
                            panelUpdate.Visible = false;
                            this.pager1.Bind();
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                );
            }
        }

        private void txtHaveTime_KeyPress(object sender, KeyPressEventArgs e)
        {
            IsNumberMoney(txtHaveTime, e);
        }

        private bool IsNumberMoney(System.Windows.Forms.TextBox t, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == 8 || e.KeyChar == 13))
            {
                return true;
            }
            else
            {
                DialogNotify diaSecc = new DialogNotify("只能输入数字！");
                diaSecc.ShowDialog();
                e.Handled = true;
                return false;
            }
        }

        private void dgvInfo_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvInfo.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvInfo.Rows.Count; )
                {
                    this.dgvInfo.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }
    }
}
